<?php
namespace Api\Controller;

use Think\Controller;

class PaymentController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 支付通道
     * @return [type] [description]
     */
    public function index()
    {
        $payment = D('Api/Payment', 'Service')->lists('app');
        success($payment);
    }

    /**
     * 订单请求支付
     * @return [type] [description]
     */
    public function order()
    {
        $order_no   = I('post.order_no', '', 'trim');
        $pay_method = I('post.method', '', 'trim');

        if (!$order_no) {
            error(8056, '订单号错误');
        }

        if (!in_array($pay_method, ['wechat', 'alipay'])) {
            error(8067, '支付方式有误');
        }

        $order = M('order')
            ->field('user_id,order_no,total,is_pay,status')
            ->where(['order_no' => $order_no])
            ->find();

        $Order = D('Api/Order', 'Service');
        if ($order['is_pay'] || $order['status'] != $Order::UN_PAY) {
            error(8057, '该订单无法再次支付');
        }

        $payment = D('Common/payment', 'Service')->driver($pay_method);

        $res = $payment->app('订单支付', $order['order_no'], $order['total'], 'https://www.baidu.com');
        if (!$res) {
            error(8068, $payment->error);
        }

        success($package);
    }

}
